package cn.cloudplug.friendsrecommend;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.fragment.LoginFragment;
import cn.cloudplug.friendsrecommend.fragment.RegFragment;

public class LoginActivity extends FragmentActivity implements OnClickListener,OnPageChangeListener{
	public static final int LOGIN=100;
	public static final int REGIST=101;
	private ViewPager viewPager;
	private List<Fragment> fragments=new ArrayList<Fragment>();
	private TextView tv_login;
	private TextView tv_reg;
	private TextView tv_close;
	public JSONObject json;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_reg);
		setView();
		setListener();
	}
	private void setListener() {
		// TODO Auto-generated method stub
		tv_login.setOnClickListener(this);
		tv_reg.setOnClickListener(this);
		tv_close.setOnClickListener(this);
	}
	private void setView() {
		// TODO Auto-generated method stub
		Intent intent=getIntent();
		String action=intent.getStringExtra("action");
		tv_login=(TextView) findViewById(R.id.tv_login);
		tv_login.setSelected(true);
		tv_reg=(TextView) findViewById(R.id.tv_reg);
		tv_close=(TextView) findViewById(R.id.tv_close);
		viewPager=(ViewPager) findViewById(R.id.login_reg_iewPager);
		Fragment loginFragment=new LoginFragment();
		Fragment regFragment=new RegFragment();
		fragments.add(loginFragment);
		fragments.add(regFragment);
		MyLoginRegPagerAdapter adapter=new MyLoginRegPagerAdapter(getSupportFragmentManager());
		viewPager.setAdapter(adapter);
		viewPager.setOnPageChangeListener(this);
		if(action.equals("login")){
			viewPager.setCurrentItem(0);
		}else if(action.equals("reg")){
			viewPager.setCurrentItem(1);
		}
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_login:
			viewPager.setCurrentItem(0);
			break;
		case R.id.tv_reg:
			viewPager.setCurrentItem(1);
			break;
		case R.id.tv_close:
			finish();
			overridePendingTransition(R.anim.activity_out,R.anim.b_activity_out);
			break;
		}
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
		overridePendingTransition(R.anim.activity_out,R.anim.b_activity_out);
	}
	
	class MyLoginRegPagerAdapter extends FragmentPagerAdapter{

		public MyLoginRegPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragments.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragments.size();
		}
		
	}


	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		Log.i("TAG", "onPageScrollStateChanged"+arg0);
	}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		Log.i("TAG", "onPageScrolled"+arg0);
	}
	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		Log.i("TAG", "onPageSelected"+arg0);
		switch (arg0) {
		case 0:
			tv_login.setSelected(true);
			tv_reg.setSelected(false);
			break;
		case 1:
			tv_login.setSelected(false);
			tv_reg.setSelected(true);
		}
	}
	
	

	

	
}
